package com.msb.serviceprice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msb.commons.constant.CommonStatusEnum;
import com.msb.commons.dto.ForecastPriceDTO;
import com.msb.commons.dto.PriceRule;
import com.msb.commons.response.DirectionResponse;
import com.msb.commons.response.ForecastPriceResponse;
import com.msb.commons.response.ResponseResult;
import com.msb.serviceprice.feign.ServiceMapClient;
import com.msb.serviceprice.mapper.PriceRuleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: chshh
 * @version: 1.0
 */
@Service
@Slf4j
public class ForecastPriceService {
    @Resource
    private ServiceMapClient serviceMapClient;

    @Resource
    private PriceRuleMapper priceRuleMapper;

    public ResponseResult ForecastPrice(String depLongitude, String depLatitude,
                                        String destLongitude, String destLatitude,
                                        String cityCode,String vehicleType){

        ForecastPriceDTO forecastPriceDTO = new ForecastPriceDTO();
        forecastPriceDTO.setDepLatitude(depLatitude);
        forecastPriceDTO.setDepLongitude(depLongitude);
        forecastPriceDTO.setDestLatitude(destLatitude);
        forecastPriceDTO.setDestLongitude(destLongitude);
        forecastPriceDTO.setCityCode(cityCode);
        forecastPriceDTO.setVehicleType(vehicleType);

        log.info("调用地图服务,查询距离和时长");
        ResponseResult<DirectionResponse> driving = serviceMapClient.driving(forecastPriceDTO);
        Integer distance = driving.getData().getDistance();
        Integer duration = driving.getData().getDuration();

        log.info("读取计价规则");

        QueryWrapper<PriceRule> wrapper = new QueryWrapper<>();
        wrapper.eq("city_code",cityCode)
                .eq("vehicle_type",vehicleType)
                .orderByDesc("fare_version");
        List<PriceRule> priceRules = priceRuleMapper.selectList(wrapper);
        if (priceRules.size() == 0){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(),CommonStatusEnum.PRICE_RULE_EMPTY.getValue());
        }
        PriceRule priceRule = priceRules.get(0);


        log.info("根据距离,时长和计价规则,计算价格");
        Double price = getPrice(distance, duration, priceRule);

        ForecastPriceResponse forecastPriceResponse = new ForecastPriceResponse();
        forecastPriceResponse.setPrice(price);
        forecastPriceResponse.setCityCode(cityCode);
        forecastPriceResponse.setVehicleType(vehicleType);
        forecastPriceResponse.setFareType(priceRule.getFareType());
        forecastPriceResponse.setFareVersion(priceRule.getFareVersion());
        return ResponseResult.success(forecastPriceResponse);
    }

    private Double getPrice(Integer distance,Integer duration,PriceRule priceRule ){
        //BigDecimal
        BigDecimal price = new BigDecimal(0);

        //起步价
        Double startFare = priceRule.getStartFare();
        BigDecimal startFareDecimal = new BigDecimal(startFare);


        //里程费
        //总里程 m
        BigDecimal distanceDecimal = new BigDecimal(distance);
        //总里程 km
        BigDecimal distanceMileDecimal = distanceDecimal.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP);
        //起步里程
        Integer startMile = priceRule.getStartMile();
        BigDecimal startMileDecimal = new BigDecimal(startMile);
        //差值里程
        Double distanceSubtract = distanceMileDecimal.subtract(startMileDecimal).doubleValue();
        //最终收费的里程数 km
        Double mile = distanceSubtract < 0?0:distanceSubtract;
        BigDecimal mileDecimal = new BigDecimal(mile);
        //计程单价 元/km
        Double unitPricePerMile = priceRule.getUnitPricePerMile();
        BigDecimal unitPricePerMileDecimal = new BigDecimal(unitPricePerMile);
        //里程价格
        BigDecimal mileFare = mileDecimal.multiply(unitPricePerMileDecimal);

        //时长费
        //总时长 秒
        BigDecimal time = new BigDecimal(duration);
        //总时长 分钟
        BigDecimal timeDecimal = time.divide(new BigDecimal(60), 2, BigDecimal.ROUND_HALF_UP);
        //计时单价  元/分钟
        Double unitPricePerMinute = priceRule.getUnitPricePerMinute();
        BigDecimal unitPricePerMinuteDecimal = new BigDecimal(unitPricePerMinute);
        //最终的时长收费
        BigDecimal timeFare = timeDecimal.multiply(unitPricePerMinuteDecimal);


        //总费用= 起步价+里程费+时长费
        price= price.add(startFareDecimal).add(mileFare).add(timeFare).setScale(2,BigDecimal.ROUND_UP);

        return price.doubleValue();
    }
}